import grails.converters.*

class JankenController {
    def jankenService
    Random rand = new Random()

    def index = {
        render(view:"view")
    }

    def play = {
        def playerIdx = Integer.valueOf(params.turn)
        def opponentIdx = rand.nextInt(3)
        def opponent = Janken.values()[opponentIdx]
        def player = Janken.values()[playerIdx]
        def result = [:]
        result["winner"] = jankenService.play(player, opponent)
        result["player"] = player.toString()
        result["opponent"] = opponent.toString()
        result["playerScore"] = jankenService.playerScore
        result["opponentScore"] = jankenService.opponentScore

        def type = params.type?:'xml'

        if(type == 'xml'){
            render result as XML
        }else{
            render result as JSON
        }
    }

    def restart = {
        jankenService.restart()
        render(view:"view")
    }
}
